package com.bianmaba.beans.datacollection;

import com.bianmaba.spring.data.jpa.domain.annotations.QueryCacheable;
import com.bianmaba.spring.data.jpa.domain.entity.UuidIdentityEntity;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.struts2.json.annotations.JSON;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import java.util.Date;


@Entity
@DynamicUpdate()
@DynamicInsert()
//此注解重新定义为是否开始查询缓存
@QueryCacheable
//此注解表示此类支持二级缓存，缓存名称为：defaultEntityCache
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Data
@NoArgsConstructor
public class CollectionSchedule extends UuidIdentityEntity {
    private String name;
    private String description;
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date startDatetime;
    private String cron;
    @OneToOne(optional = false)
    @JoinColumn(name = "collection_task_id")
    private CollectionTask task;
    private String projectId;

    private Boolean disabled;

    @JSON(format = "yyyy-MM-dd HH:mm:ss")
    public Date getStartDatetime() {
        return startDatetime;
    }

    public String getStartTime() {
        if (this.startDatetime != null) {
            return DateFormatUtils.format(this.startDatetime, "HH:mm:ss");
        }
        return null;
    }

    public String getStartDate() {
        if (this.startDatetime != null) {
            return DateFormatUtils.format(this.startDatetime, "yyyy-MM-dd");
        }
        return null;
    }
}
